package net.zjitc.Interceptor;

import com.alibaba.fastjson2.JSONObject;
import io.jsonwebtoken.Claims;
import net.zjitc.context.LocalThreadHolder;
import net.zjitc.pojo.api.ApiResult;
import net.zjitc.pojo.api.Result;
import net.zjitc.utils.JwtUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;

//token拦截器，做请求拦截
//所有请求执行之前，都会被该拦截器拦截  token校验通过则正常放行请求，否则直接返回
public class JwtInterceptor implements HandlerInterceptor {

    //前置拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestMethod = request.getMethod();
        if ("OPTIONS".equals(requestMethod)) {
            return true;
        }
        String requestURI = request.getRequestURI();
        // 登录及错误等请求不做拦截
        if (requestURI.contains("/login") || requestURI.contains("/error") || requestURI.contains("/file") || requestURI.contains("/register")) {
            return true;
        }
        String token = request.getHeader("token");
        Claims claims = JwtUtil.fromToken(token);
        // 解析不成功，直接退回,访问后续资源的可能性都没有
        if (claims == null) {
            Result<String> error = ApiResult.error("身份认证异常，请先登录");
            response.setContentType("application/json;charset=UTF-8");
            Writer stream = response.getWriter();
            // 将失败信息输出
            stream.write(JSONObject.toJSONString(error));
            stream.flush();
            stream.close();
            return false;
        }
        Integer userId = claims.get("id", Integer.class);
        Integer roleId = claims.get("role", Integer.class);
        LocalThreadHolder.setUserId(userId, roleId);
        return true;
    }
}
